const express = require('express');
// const dx = require('../service/dx');
// const ca = require('../service/dx/ca');
const log = require('../../tools/log');
const Stopwatch = require('../util/stopwatch');
const {v4: uuid} = require('uuid');
const router = express.Router();

const {akamaiValidateSpawn} = require("../service");




router.post('/akamai/validate', async (req, res, next) => {
    const traceId = uuid().split("-").join("");
    log.info("[Akamai][%s]【请求】", traceId, JSON.stringify(req.body).slice(0,1000));
    const result = {success: true};
    const stopwatch = new Stopwatch();
    let callback = async (msg) => {
        try {
            res.send(msg);
        }catch (err){
            log.warn("[Akamai][%s] : ", traceId, msg);
        }

        log.info("[Akamai][%s]【响应】耗时: %sms", traceId, stopwatch.elapsed(), msg);
    };
    if (!(req.body.ua && req.body.ua)) {
        result.success = false;
        result.message = '请求参数不合法';
        log.warn("[Akamai][%s]【响应】耗时: %sms", traceId, stopwatch.elapsed(), JSON.stringify(result));
        res.send(result);
        return;
    }
    try {
        akamaiValidateSpawn(req.body.captchaId, req.body.referer, req.body.ua, req.body.cookies, req.body.jsContent,
            req.body.html, traceId, callback);
    } catch (err) {
        result.success = false;
        result.message = err.toString();
        log.warn("[Akamai][%s]【响应】耗时: %sms", traceId, stopwatch.elapsed(), JSON.stringify(result), err);
        res.send(result);
    }
})